﻿using CCWin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TelephoneYiShou;

namespace employee
{
    public partial class empChange : Skin_Mac
    {
        public empChange()
        {
            InitializeComponent();
        }
        int i, start;//i表示总行数，start表示当前位置
        int size = 10;//size表示一页显示的数量        
        string str1 = "select * from Employee";
        string str = "server=.;database = TelephoneYiShou;uid = sa;pwd = 123456";

        private void btn_change_Click(object sender, EventArgs e)
        {
            //修改员工信息
            string id = dgv_sheet.SelectedRows[0].Cells["Column1"].Value.ToString();
            string empname = txt_name.Text;
            string salary = txt_salary.Text;
            string sex = cmb_sex.Text;
            string address = txt_address.Text;
            string dept = cmb_dept.Text;
            string phone = txt_tel.Text;
            string code = txt_code.Text;
            string birth = dtp_birth.Text;
            if (string.IsNullOrEmpty(empname) || string.IsNullOrEmpty(salary) ||
                string.IsNullOrEmpty(sex) || string.IsNullOrEmpty(address) ||
                string.IsNullOrEmpty(dept) || string.IsNullOrEmpty(phone) ||
                string.IsNullOrEmpty(code) || string.IsNullOrEmpty(birth))
            {
                MessageBoxEx.Show("信息输入不完整！");
                return;//直接返回
            }
            string sql = string.Format("update Employee set EmployeeName='{0}'," +
                "Salary='{1}',Gender='{2}',Phone='{3}',Adderss='{4}',Dept='{5}',Pwd='{6}',Date='{7}' where EmployeeID='{8}'",
                 empname, salary, sex, phone,address,dept,code, Convert.ToDateTime(birth),id);

            if (DBHelper.ExecuteNonQuery(sql))
            {
                MessageBoxEx.Show("员工信息修改成功！");
                string strsql = "select * from Employee";
                DataTable dt = DBHelper.GetDataTable(strsql);
                dgv_sheet.DataSource = dt;
            }
            else
            {
                MessageBoxEx.Show("修改失败,请联系管理员!");
            }
        }

        private void btn_del_Click(object sender, EventArgs e)
        {
            //删除
            string select_id = dgv_sheet.SelectedRows[0].Cells["Column1"].Value.ToString();//选择的当前行第一列的值，也就是ID
            string sql = "delete from Employee where EmployeeID='" + select_id + "'";//sql删除语句
            DialogResult dr =  MessageBoxEx.Show("您确定要删除编号为" + select_id + "的信息吗？", "警告", MessageBoxButtons.YesNo);
            if (dr==DialogResult.Yes)
            {
                try
                {
                    if (DBHelper.ExecuteNonQuery(sql))
                    {
                        MessageBoxEx.Show("确定要删除吗？", "系统提示：");
                        string strsql = "select * from Employee";
                        DataTable dt = DBHelper.GetDataTable(strsql);
                        dgv_sheet.DataSource = dt;
                        i = dt.Rows.Count;//获取数据的条数
                    }
                    else
                    {
                        MessageBoxEx.Show("失败");
                    }
                }
                catch (Exception)
                {

                    MessageBoxEx.Show("请先删除该员工的有关订单");
                }
                
            }
            
        }

        private void empClear_Load(object sender, EventArgs e)
        {
            string strsql = "select * from Employee";
            DataTable dt = DBHelper.GetDataTable(strsql);
            dgv_sheet.DataSource = dt;
            i = dt.Rows.Count;//获取数据的条数
        }
        private void btn_exit_Click(object sender, EventArgs e)
        {
            //退出
            this.Close();
        }

        private void dgv_sheet_Click(object sender, EventArgs e)
        {
            txt_name.Text = dgv_sheet.SelectedRows[0].Cells["column2"].Value.ToString();
            txt_salary.Text = dgv_sheet.SelectedRows[0].Cells["column3"].Value.ToString();
            cmb_sex.Text = dgv_sheet.SelectedRows[0].Cells["column4"].Value.ToString();
            txt_tel.Text = dgv_sheet.SelectedRows[0].Cells["column5"].Value.ToString();
            txt_address.Text = dgv_sheet.SelectedRows[0].Cells["column6"].Value.ToString();
            txt_code.Text = dgv_sheet.SelectedRows[0].Cells["column7"].Value.ToString();
            cmb_dept.Text = dgv_sheet.SelectedRows[0].Cells["column8"].Value.ToString();
            dtp_birth.Text = dgv_sheet.SelectedRows[0].Cells["column9"].Value.ToString();
        }

        //首页
        private void btn_head_Click(object sender, EventArgs e)
        {
            show(0, size);//调用show函数
            start = 0;
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        //上一页
        private void btn_up_Click(object sender, EventArgs e)
        {
            if (start < 1)
            {
                MessageBoxEx.Show("已是第一页.");
            }
            else
            {
                start = start - 1;
                show(start * size, size);
            }
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }

        //下一页
        private void btn_down_Click(object sender, EventArgs e)
        {
            if (start + 1 > i / size)
            {
                MessageBoxEx.Show("已是最后页");
            }
            else
            {
                start = start + 1;
                show(start * size, size);
            }
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }


        //尾页
        private void btn_end_Click(object sender, EventArgs e)
        {
            show(i / size, i - (i / size) * size);
            start = i / size;
            labelcount.Text = string.Format("第{0}页/共{1}页 共{2}条数据", start + 1, i / size + 1, i);//显示数据
        }


        //显示i到j的数据
        public void show(int i, int j)
        {
            SqlConnection sqlcon = new SqlConnection(str);
            sqlcon.Open();
            SqlCommand sqlcmd = new SqlCommand(str1, sqlcon);
            SqlDataAdapter sqlada = new SqlDataAdapter(sqlcmd);
            DataSet ds = new DataSet();
            sqlada.Fill(ds, i, j, "[SellGoods]");//把数据库表中的i-j的记录数用来填充数据集
            dgv_sheet.DataSource = ds.Tables["[SellGoods]"];
            ds = null;//清空数据集
        }
    }
}
